#include "runshoptablewidget.h"
#include "setting.h"
#include "QPushButton"
#include "utils/deviceutils.h"
#include "QComboBox"
#include "utils/runshopgoodsmanager.h"

RunshopTableWidget::RunshopTableWidget(QWidget *parent) : QTableWidget(parent)
{
    this->parent = parent;
}

RunshopTableWidget::~RunshopTableWidget()
{

}

void RunshopTableWidget::init(){
    QStringList list;
    list<< "角色序号" <<"本帮城市" << "本帮校尉坐标" << "本帮城市索引" << "对方城市" << "对方校尉坐标" << "对方城市索引" << "购买商品"<< "买卖时辰" << "操作";
    QStringList tip;
    tip<< "与角色管理中的序号对应,表示角色管理中的此角色进行此项跑商设置" <<"本帮校尉所在的城市名称" << "本帮校尉的坐标" << "本帮城市在校尉对话框中的第几个" << "对方的校尉所在城市名称" << "对方校尉的坐标(帮派与帮派之间跑商需要设置此项)" << "对方城市在校尉对话框中的第几个(帮派与帮派之间跑商需要设置此项)" << "在对方帮派购买的商品（帮派与帮派之间跑商需要设置此项）"<< "商品最高价买卖时辰" << "操作";

    setHorizontalHeaderLabels(list);
    setFocusPolicy(Qt::FocusPolicy::NoFocus);
    setColumnWidth(2, 60);
    setColumnWidth(5, 60);
    QHeaderView *headerView = horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch); //自适应列宽
    headerView->setMinimumHeight(40);
    headerView->setStyleSheet("QHeaderView::section {background-color: #ededed; border-bottom:1px solid #000000}");
    headerView->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
    headerView->setHighlightSections(false);
    for(int i = 0 ; i < list.size(); i++){
        QTableWidgetItem *item = new QTableWidgetItem(list.at(i));
        QFont font = this->font();
        font.setBold(true);
        item->setFont(font);

        item->setFlags(Qt::ItemFlag::NoItemFlags);
        item->setSelected(false);
        item->setBackground(Qt::gray);
        if(i != 5 && i != 6 && i != list.size() -1){
            item->setForeground(Qt::red);
        }
        item->setToolTip(tip.at(i));
        setHorizontalHeaderItem(i, item);

    }
    QList<RunShopBean> runShopBeans;
    readRunshopBeansSetting(runShopBeans);
    if(!runShopBeans.isEmpty()){
        for(RunShopBean runshopBean : runShopBeans){
            creatNewRow(runshopBean.num, runshopBean.groupCity,runshopBean.groupXiaoWeiLocation ,runshopBean.groupIndex, runshopBean.remoteCity,runshopBean.remoteXiaoWeiLocation ,runshopBean.remoteCityIndex , runshopBean.remoteBuyGoods, runshopBean.time);
        }
    }
}

int RunshopTableWidget::creatNewRow(int num, QString groupCity, QString groupXiaoWeiLocation, int groupIndex, QString remoteCity, QString remoteXiaoWeiLocation, int remoteCityIndex, QString remoteBuyGoods, QString time)
{
    if(!isCreateTableFinish())return -1;
    int row = rowCount();
    setRowCount(row  + 1);

    //序号
    QTableWidgetItem *item111 = new QTableWidgetItem();
    item111->setTextAlignment(Qt::AlignCenter);
    item111->setSelected(false);
    item111->setText(num == 0 ? "" :QString::number(num));
    this->setItem(row ,numType, item111);

    //本帮城市
    QTableWidgetItem *item0 = new QTableWidgetItem();
    item0->setTextAlignment(Qt::AlignCenter);
    item0->setSelected(false);
    item0->setText(groupCity);
    this->setItem(row ,groupCityType, item0);


    //本帮校尉坐标
    QComboBox *groupXiaoWeiBox = new QComboBox(this);
    QStringList xiaoWeiLocations = RunShopGoodsManager::getInstance()->getXiaoWeiLocations();
    groupXiaoWeiBox->addItems(xiaoWeiLocations);
    groupXiaoWeiBox->setCurrentIndex(xiaoWeiLocations.indexOf(groupXiaoWeiLocation));
    groupXiaoWeiBox->setMaxCount(12);
    groupXiaoWeiBox->setEditable(false);
    setCellWidget(row ,groupLocationType, groupXiaoWeiBox);

    //本帮城市索引
    QTableWidgetItem *item12 = new QTableWidgetItem();
    item12->setTextAlignment(Qt::AlignCenter);
    item12->setSelected(false);
    item12->setText(groupIndex == 0 ? "" : QString::number(groupIndex));
    this->setItem(row ,groupIndexType, item12);


    //对方城市名称
    QTableWidgetItem *item13 = new QTableWidgetItem();
    item13->setTextAlignment(Qt::AlignCenter);
    item13->setSelected(false);
    item13->setText(remoteCity);
    this->setItem(row ,remoteCityType, item13);

    //对方校尉坐标
    QComboBox *remoteXiaoWeiBox = new QComboBox(this);
    remoteXiaoWeiBox->addItems(xiaoWeiLocations);
    remoteXiaoWeiBox->setCurrentIndex(xiaoWeiLocations.indexOf(remoteXiaoWeiLocation));
    remoteXiaoWeiBox->setMaxCount(12);
    remoteXiaoWeiBox->setEditable(false);
    setCellWidget(row ,remoteXiaoWeiLocationType, remoteXiaoWeiBox);


    QTableWidgetItem *item15 = new QTableWidgetItem();
    item15->setTextAlignment(Qt::AlignCenter);
    item15->setSelected(false);
    item15->setText(remoteXiaoWeiLocation);
    this->setItem(row ,remoteXiaoWeiLocationType, item15);


    //对方城市索引

    QTableWidgetItem *item14 = new QTableWidgetItem();
    item14->setTextAlignment(Qt::AlignCenter);
    item14->setSelected(false);
    item14->setText(remoteCityIndex == 0 ? "" : QString::number(remoteCityIndex));
    this->setItem(row ,remoteCityIndexType, item14);

    //购买商品
    QComboBox *box = new QComboBox(this);
    QStringList goods = RunShopGoodsManager::getInstance()->getGoodsNames();
    box->addItems(goods);
    box->setCurrentIndex(goods.indexOf(remoteBuyGoods));
    box->setMaxCount(5);
    box->setEditable(false);
    setCellWidget(row ,remoteBuyGoodsType, box);

    //买卖时辰
    QComboBox *timeBox = new QComboBox(this);
    QStringList times = RunShopGoodsManager::getInstance()->getTimes();
    timeBox->addItems(times);
    timeBox->setCurrentIndex(times.indexOf(time));
    timeBox->setMaxCount(12);
    timeBox->setEditable(false);
    setCellWidget(row ,timeType, timeBox);

    //操作 删除
    QPushButton *deleteButton = new QPushButton(this);
    deleteButton->setText("删除");
    deleteButton->setStyleSheet("color:red");
    setCellWidget(row, deleteType, deleteButton);
    connect(deleteButton, &QPushButton::clicked,this,[&](){
        this->removeRow(currentRow());
    }); // 再连接信号
    return 0;
}

void RunshopTableWidget::readRunshopBeansSetting(QList<RunShopBean> &runShopBeans)
{
    QString json = Setting::getInstance()->getRunShopJson();
    runShopBeans = RunShopBean::jsonToArray(json.toUtf8());
}

bool RunshopTableWidget::getAllTableBeans(QList<RunShopBean*> *allRunShopBeans, bool isNeedFindPath)
{
    if(allRunShopBeans == nullptr){
        return false;
    }
    qDeleteAll(allRunShopBeans->begin(), allRunShopBeans->end());
    allRunShopBeans->clear();
    for(int i = 0 ; i < rowCount() ; i++){
        RunShopBean *runShopBean = new RunShopBean();
        if(getBean(i, runShopBean, isNeedFindPath)){
            allRunShopBeans->append(runShopBean);
        }
    }
    if(allRunShopBeans->isEmpty()){
        return false;
    }
    return true;
}

bool RunshopTableWidget::getBean(int row, RunShopBean *runshopBean , bool isNeedFindPath){
    bool isOk = false;
    QString numStr = item(row ,numType)->text();
    int num = numStr.toInt(&isOk);
    if(!isOk){
        emit logEvent("角色序号设置错误");
        return false;
    }
    runshopBean->num = num;

    QString groupCity = item(row ,groupCityType)->text();
    runshopBean->groupCity = groupCity;




    QComboBox *groupXiaoWeiComboBox = (QComboBox*)cellWidget(row,groupLocationType);
    QString groupXiaoWeiLocation = groupXiaoWeiComboBox->currentText();
    if(!groupXiaoWeiLocation.isEmpty() && groupXiaoWeiLocation != "无"){
        runshopBean->groupXiaoWeiLocation = groupXiaoWeiLocation;
    }else{
         emit logEvent("请选择本帮校尉位置");
        return false;
    }

    QString groupIndexStr = item(row ,groupIndexType)->text();
    int groupIndex = groupIndexStr.toInt(&isOk);
    if(!isOk){
        emit logEvent("本帮城市索引设置错误");
        return false;
    }
    runshopBean->groupIndex = groupIndex;

    QString remoteCity = item(row ,remoteCityType)->text();
    runshopBean->remoteCity = remoteCity;


    QComboBox *remoteXiaoWeiComboBox = (QComboBox*)cellWidget(row,remoteXiaoWeiLocationType);
    QString remoteXiaoWeiLocation = remoteXiaoWeiComboBox->currentText();
    if(!remoteXiaoWeiLocation.isEmpty() && remoteXiaoWeiLocation != "无"){
        runshopBean->remoteXiaoWeiLocation = remoteXiaoWeiLocation;
    }


    QString remoteCityIndexStr = item(row ,remoteCityIndexType)->text();
    if(!remoteCityIndexStr.isEmpty()){
        int remoteCityIndex = remoteCityIndexStr.toInt(&isOk);
        if(!isOk){
            emit logEvent("对方帮派城市坐标索引设置错误");
            return false;
        }
        runshopBean->remoteCityIndex = remoteCityIndex;
    }

    QComboBox *comboBox = (QComboBox*)cellWidget(row,remoteBuyGoodsType);
    QString godosName = comboBox->currentText();
    if(!godosName.isEmpty()){
        RunShopGoods *goods = RunShopGoodsManager::getInstance()->getGoods(godosName);
        runshopBean->remoteBuyGoods = godosName;
        runshopBean->remoteStartScalar = goods->sellStartScalar;
        runshopBean->remoteEndScalar = goods->sellEndScalar;
        runshopBean->remoteSellShopImg = goods->sellImg;
        runshopBean->remoteBuyShopImg = goods->buyImg;
        runshopBean->groupBuyGoodsPrices = goods->remoteBuyGoodsPrices;
        runshopBean->groupSellGoodsPrices = goods->groupSellGoodsPrices;
    }else{
         emit logEvent("请选择购买商品");
    }

    //买卖商品最高价时辰
    QComboBox *timeBox = (QComboBox*)cellWidget(row,timeType);
    QString time = timeBox->currentText();
    if(!time.isEmpty() && !time.isNull()){
        runshopBean->time = time;
    }else{
        emit logEvent("请选择买卖时辰");
    }

    if(isNeedFindPath){
        runshopBean->setLine();
    }
    return true;
}

bool RunshopTableWidget::saveBeans2File(){
    QList<RunShopBean *> allRunshopBeans;
    if(getAllTableBeans(&allRunshopBeans)){
        QByteArray a= RunShopBean::ArraytoJson(&allRunshopBeans);
        Setting::getInstance()->setRunShopJson(QString(a));
        return true;
    }
    qDeleteAll(allRunshopBeans.begin(), allRunshopBeans.end());
    return false;
}

bool RunshopTableWidget::isCreateTableFinish(){
    int row = rowCount();
    if(row == 0) return true;
    bool isFinish = true;
    for(int i = 0 ; i< columnCount() - 1; i++){
        if(i == groupLocationType || i == remoteXiaoWeiLocationType || i == remoteCityIndexType || i == remoteBuyGoodsType || i== timeType){
            continue;
        }
        QString content = item(row - 1,i)->text();
        isFinish = isFinish && !content.isEmpty();
    }
    return isFinish;
}

